package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.SysRole;
import com.coin.model.R;
import com.coin.service.SysRoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: 李 力
 * @Date: 2025/5/27
 * @Description: com.coin.controller
 * @version: 1.0
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/roles")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * @Description: 角色分页查询
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: current  size   name
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "条件分页查询")
    @PreAuthorize("hasAuthority('sys_role_query')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的大小"),
            @ApiImplicitParam(name = "name", value = "角色名称"),
    })
    public R<Page<SysRole>> findByPage(@ApiIgnore Page<SysRole> page, String name) {
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(sysRoleService.findByPage(page, name));
    }

    /**
     * @Description: 新增角色
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: sysRole
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个角色")
    @PreAuthorize("hasAuthority('sys_role_create')")
    public R add(@RequestBody @Validated SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);
        return save ? R.ok("新增成功") : R.fail("新增失败");
    }

    /**
     * @Description: 删除角色数据
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: ids
     * @return: R
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除一个角色数据")
    @PreAuthorize("hasAuthority('sys_role_delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除角色的id的集合")
    })
    public R delete(@RequestBody String[] ids) {
        boolean result = sysRoleService.deleteRoleByIds(ids);
        return result ? R.ok("删除成功") : R.fail("删除失败");
    }
}
